<?php
if (file_exists('install/install.lock'))
{
	exit('已经安装');
}
$step = intval($_POST['step']);
if ($step == 2)
{
	include('datacollection.html');
}
else if ($step == 3)
{
	$host = trim($_POST['host']);
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$database = trim($_POST['database']);
	importData($host, $username, $password, $database);
}
else 
{
	include('introduction.html');
}

function importData($host, $username, $password, $database)
{
	$connid = mysql_connect($host, $username, $password);
	if (!$connid)
	{
		die( "数据库连接错误!" );
	}
	//设置连接字符集
	mysql_query("set names utf8");
	//检查数据库是否存在
	$dbs = mysql_query("show databases");
	$exists = false;
	while ($rs = mysql_fetch_array($dbs))
	{
		if ($row['Database'] == $database)
		{
			$exists = true;
			break;
		}
	}
	if (!$exists)
	{
		mysql_query("create database $database");
	}
	mysql_select_db($database);
	//读取sql文件
	$content = file_get_contents('example.sql');
	$arr = explode(array(';\r', ';\r\n', ';\n\r'), $content);
	foreach ($arr as $sql)
	{
		if (!empty($sql))
		{
			mysql_query($sql) or err();
		}
	}
	$f = fopen('install.lock', 'wb+');
	fwrite($f, "要重新安装,请删除本文件!");
	fclose($f);
	mysql_close();
	print "安装成功";
}

function err()
{
	$errno = mysql_errno();
	$error = mysql_error();
	mysql_close();
	include('error.php');
	exit();
}

//function 